import * as moment from "moment";
import { asyncmessagesend } from '../plugins/api.js'
import uuid from "uuid/v4";
import OSS from "ali-oss";
import { ossupload } from '../plugins/api.js'
/**
 * 检查是否有冲突
 * @param {*} list 
 * @param {*} getlist 
 */
export const conflictvalidator = (list, getlist) => {
  const {
    starttime,
    schoolclasstime,
    schooluserid,
    schoolclassroomid,
    schoolclazzid,
    ID
  } = list;
  const time = schoolclasstime.split("-");
  const start = moment(
    `${new Date(starttime).toLocaleDateString()} ${time[0]}`,
    "YYYY-MM-DD HH:mm"
  ).unix() * 1000
  const end = moment(
    `${new Date(starttime).toLocaleDateString()} ${time[1]}`,
    "YYYY-MM-DD HH:mm"
  ).unix() * 1000
  return getlist.find(v => {
    if (ID && ID == v.ID) return false;
    const t = v.schoolclasstime.split("-");
    const s = moment(
      `${new Date(v.starttime).toLocaleDateString()} ${t[0]}`,
      "YYYY-MM-DD HH:mm"
    ).unix() * 1000
    const e = moment(
      `${new Date(v.starttime).toLocaleDateString()} ${t[1]}`,
      "YYYY-MM-DD HH:mm"
    ).unix() * 1000
    const i = intersection({ s, e }, { s: start, e: end });
    if (i) {
      if (schoolclazzid == v.schoolclazzid) return true
      if (schooluserid == v.schooluserid) return true;
      if (schoolclassroomid == v.schoolclassroomid) return true;
    }
  });
}


/**
 * 检查是否有交集
 * @param {*} a 
 * @param {*} b 
 */
export const intersection = (a, b) => {
  let s = a.s >= b.s ? a.s : b.s;
  let e = a.e >= b.e ? b.e : a.e;
  if (s <= e) {
    return { s, e };
  } else {
    return false;
  }
}

/**
 * 获取时间
 * @param {*} time 
 */
export const totime = time => {
  if (time) {
    return new Date(time).toLocaleDateString();
  }
  return ""
}

export const disabledDate = time => time.getTime() < (Date.now() - 1000 * 24 * 3600) || time.getTime() > Date.now() + 1000 * 3600 * 24 * 365

/**
 * 上课点评
 * @param {number} schoolstudentid 学生id
 * @param {string} name 学生名称
 * @param {string} info 课程名称
 * @param {string} time 上课时间段
 * @param {string} username 老师名称
 * @param {string} type 上课类型
 */
export const messagesend = async (schoolstudentid, name, info, time, username, type) => {
  try {
    let gx = "您的孩子";
    await asyncmessagesend({
      template: "vLMT-upgkJQgnzXhZ16y89JmvDKnn3lHNpnKF9xwnE4",
      ID: schoolstudentid,
      data: {
        first: {
          value: `${gx}已${type}。`,
          color: "#00000"
        },
        keyword1: {
          value: `${name}`,
          color: "#00000"
        },
        keyword2: {
          value: `${time}`,
          color: "#00000"
        },
        keyword3: {
          value: `${type}`,
          color: "#00000"
        },
        keyword4: {
          value: `${username}`,
          color: "#00000"
        },
        keyword5: {
          value: `生活愉快`,
          color: "#00000"
        },
        remark: {
          value: ``,
          color: "#00000"
        }
      },
    });
  } catch (err) {
    console.log(err);
  }
}

/**
 * 下课点评
 * @param {number} schoolstudentid 学生id
 * @param {string} name 学生名称
 * @param {string} info 课程名称
 * @param {string} time 上课时间段
 * @param {string} username 老师名称
 * @param {number} ID 跳转id
 * @param {string} schoolname 学校名称
 */
export const messagesend2 = async (schoolstudentid, name, info, time, username, ID, schoolname) => {
  try {
    await asyncmessagesend({
      template: "BqWc1FIGwPW7YTUDKPnGeb7wTjf13QcWaN9oLn72_DE",
      ID: schoolstudentid,
      data: {
        first: {
          value: `${name}家长您好，您已收到一条课后评价`,
          color: "#00000"
        },
        keyword1: {
          value: `${info ? info : "编程课堂"}`,
          color: "#00000"
        },
        keyword2: {
          value: `${time}`,
          color: "#00000"
        },
        keyword3: {
          value: `${username}`,
          color: "#00000"
        },
        keyword4: {
          value: `${schoolname}`,
          color: "#00000"
        },
        remark: {
          value: `快点击详情查看孩子的精彩瞬间吧！`,
          color: "#00000"
        }
      },
      url: "https://www.hterobot.com",
      miniprogram: {
        appid: "wxba90e9098b4969bd",
        pagepath: `pages/webview/main?ID=${ID}&schoolstudentid=${schoolstudentid}`
      }
    });
  } catch (err) {
    console.log(err);
  }
}

/**
 * // 检查是否在上课
 * 
 * @param {Array} schoolstudentschoolclazz 
 * @param {string} starttime
 * @returns {boolean}
 */
export const isinclass = (schoolstudentschoolclazz, starttime) => {
  return schoolstudentschoolclazz.filter(v => {
    if (v.type !== "在读") {
      return (
        v.updatedAt > starttime &&
        new Date(v.createdAt).getTime() - 1000 * 3600 * 24 <
        new Date(starttime).getTime()
      );
    } else {
      // 获取现在在读的人是否
      return (
        new Date(v.createdAt).getTime() - 1000 * 3600 * 24 <
        new Date(starttime).getTime()
      );
    }
  });
}

/**
 * 
 * @param {number} i 
 * @param {string} name 
 * @returns {string} 
 */
export const setname = (i, name) => {
  if (name === "周") {
    return `第${i + 1}周`;
  } else if (name === "天") {
    if (i == 6) {
      return '周日'
    }
    return `周${i + 1}`;
  }
  return `${i + 1}${name}`;
};

/**
 * oss上传文件
 * @param {File[]} files 
 */
export const updateoss = async (files) => {
  const res = await ossupload();
  const client = new OSS({
    region: "oss-cn-qingdao",
    accessKeyId: res.accessKeyId,
    accessKeySecret: res.accessKeySecret,
    stsToken: res.securityToken,
    bucket: "cdn-hterobot"
  });
  const x = files[0].name.split(".");
  const name = x[x.length - 1];
  const url = `school/${uuid()}.${name}`;
  await client.put(url, files[0]);
  return `https://cdn.hterobot.com/${url}`;
}
